* Open NELDA data
use "id & q-wide.dta", clear
desc
count

keep stateid ccode country electionid year mmdd types nelda11* nelda13* ///
nelda14* nelda15* nelda29* nelda31* nelda33* nelda20*


********************
* year-level dataset
********************

preserve

sort ccode year
tab types

gen count_ass = 0
replace count_ass = 1 if types == "Constituent Assembly"
gen count_exec = 0
replace count_exec = 1 if types == "Executive"
gen count_leg = 0
replace count_leg = 1 if types == "Legislative/Parliamentary"

drop if nelda20 == "N/A"
drop if nelda20 == "unclear"

gen contest = 0
replace contest = 1 if nelda20 == "yes"
tab contest

gen count_ass_contest = 0
replace count_ass_contest = 1 if count_ass == 1 & contest == 1
tab count_ass_contest

gen count_exec_contest = 0
replace count_exec_contest = 1 if count_exec == 1 & contest == 1
tab count_exec_contest

gen count_leg_contest = 0
replace count_leg_contest = 1 if count_leg == 1 & contest == 1
tab count_leg_contest

collapse (sum) count_*, by(ccode country year)

save "nelda_year_4merge.dta", replace
count
desc

restore


***************
* Election day
***************

gen str_mmdd = string(mmdd)
gen stday = substr(str_mmdd,-2,2)
gen len_mmdd = length(str_mmdd)
tab len_mmdd
gen stmo = "."
replace stmo = substr(str_mmdd,1,2) if len_mmdd == 4
replace stmo = substr(str_mmdd,1,1) if len_mmdd == 3
tab stmo
* br str_mmdd stmo stday
gen styr = string(year)

egen stdate = concat(stmo stday styr), punct(/)
gen day = date(stdate,"MDY")
* br stmo stday styr stdate elec_date


***************************
* year-month level dataset
***************************

preserve

gen count_ass = 0
replace count_ass = 1 if types == "Constituent Assembly"
gen count_exec = 0
replace count_exec = 1 if types == "Executive"
gen count_leg = 0
replace count_leg = 1 if types == "Legislative/Parliamentary"

drop if nelda20 == "N/A"
drop if nelda20 == "unclear"

gen contest = 0
replace contest = 1 if nelda20 == "yes"
tab contest

gen count_ass_contest = 0
replace count_ass_contest = 1 if count_ass == 1 & contest == 1
tab count_ass_contest

gen count_exec_contest = 0
replace count_exec_contest = 1 if count_exec == 1 & contest == 1
tab count_exec_contest

gen count_leg_contest = 0
replace count_leg_contest = 1 if count_leg == 1 & contest == 1
tab count_leg_contest

destring stmo, replace
ren stmo month

collapse (sum) count_*, by(ccode country year month)

save "nelda_month_4merge.dta", replace
count
desc

restore



*******************************
* Different types of elections
*******************************

tab types

gen count_ass = 0
replace count_ass = 1 if types == "Constituent Assembly"
gen count_exec = 0
replace count_exec = 1 if types == "Executive"
gen count_leg = 0
replace count_leg = 1 if types == "Legislative/Parliamentary"

drop if nelda20 == "N/A"
drop if nelda20 == "unclear"

gen contest = 0
replace contest = 1 if nelda20 == "yes"
tab contest

tab count_ass contest
tab count_exec contest
tab count_leg contest

gen count_ass_contest = 0
replace count_ass_contest = 1 if count_ass == 1 & contest == 1
tab count_ass_contest

gen count_exec_contest = 0
replace count_exec_contest = 1 if count_exec == 1 & contest == 1
tab count_exec_contest

gen count_leg_contest = 0
replace count_leg_contest = 1 if count_leg == 1 & contest == 1
tab count_leg_contest

* create no contest variables

gen count_ass_NOcontest = 0
replace count_ass_NOcontest = 1 if count_ass == 1 & contest == 0
tab count_ass_NOcontest

gen count_exec_NOcontest = 0
replace count_exec_NOcontest = 1 if count_exec == 1 & contest == 0
tab count_exec_NOcontest

gen count_leg_NOcontest = 0
replace count_leg_NOcontest = 1 if count_leg == 1 & contest == 0
tab count_leg_NOcontest

collapse (sum) count_*, by(ccode country day)

save "nelda_4merge.dta", replace



